package com.example.homework_1;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;

@WebServlet(name = "CartServlet",urlPatterns = "/CartServlet")
public class CartServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
        int totalprice = 0;
        String backURL = "ListCakeServlet";
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        List<Cake> cart = null;
        boolean purFlag = true;
        HttpSession session = req.getSession(false);
        if (session == null) {
            purFlag = false;
        } else {
            cart = (List) session.getAttribute("cart");
            if (cart == null) {
                purFlag = false;
            }
        }
    if (!purFlag) {
        out.write("对不起没有购买任何商品!<br>");
    } else {
        out.write("您购买的蛋糕有:<br>");
        double price = 0;
        for (Cake cake : cart) {
            out.write(cake.getName() + "<br>");
            totalprice += cake.getPrice();
        }
    }
        out.write("总价:" + totalprice);
        out.write("<br><a href=" + backURL + ">返回</a>");
    }
}
